package com.shinbada;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.shinbada.common.Common;
import net.hasor.spring.boot.EnableHasor;
import net.hasor.spring.boot.EnableHasorWeb;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author initcap
 * @date 2020/11/23 13:24
 */
@EnableHasor
@EnableHasorWeb
@EnableWebMvc
@MapperScan({"com.shinbada.**.mapper"})
@SpringBootApplication(scanBasePackages = {"com.shinbada"},
        exclude = {
                DruidDataSourceAutoConfigure.class,
                org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
        }
)
public class StarterApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(StarterApplication.class, args);
    }

    /**
     * 其中 dataSource 框架会自动为我们注入
     *
     * @param dataSource 数据源
     * @return 事务管理器
     */
    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty(Common.Database.ID_PROVIDER_ORACLE_KEY, Common.Database.ORACLE_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_MYSQL_KEY, Common.Database.MYSQL_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_DB2_KEY, Common.Database.DB2_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_DERBY_KEY, Common.Database.DERBY_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_H2_KEY, Common.Database.H2_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_HSQL_KEY, Common.Database.HSQL_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_INFORMIX_KEY, Common.Database.INFORMIX_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_MSSQL_KEY, Common.Database.MSSQL_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_SQLSERVER_KEY, Common.Database.MSSQL_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_POSTGRESQL_KEY, Common.Database.POSTGRE_DB);
        properties.setProperty(Common.Database.ID_PROVIDER_SYBASE_KEY, Common.Database.SYBASE);
        properties.setProperty(Common.Database.ID_PROVIDER_HANA_KEY, Common.Database.HANA_DB);
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(StarterApplication.class);
    }


}
